----------------------------------------------------------------------------------------------------------------------------------
--[[


]]--
----------------------------------------------------------------------------------------------------------------------------------
---
    local function GetReplica(self)
        return self.inst.replica.hutao_com_cosmic_hat_sys_for_player or self.inst.replica._.hutao_com_cosmic_hat_sys_for_player
    end
    local function set_hat(self,hat)
        local replica = GetReplica(self)
        if replica then
            replica:SetHat(hat)
        end
    end
    local function set_drying_rack(self,drying_rack)
        local replica = GetReplica(self)
        if replica then
            replica:SetDryingRack(drying_rack)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_cosmic_hat_sys_for_player = Class(function(self, inst)
    self.inst = inst

    self.DataTable = {}
    self.TempTable = {}
    self._onload_fns = {}
    self._onsave_fns = {}


    -- inst:ListenForEvent("ms_playerleft",function(world,player)        
    -- end,TheWorld)

    self.hat = nil                  --- 链接的帽子
    self.linked_drying_rack = nil   --- 链接的晾晒架
    ------------------------------------------------------------
    ---- 玩家离开当前世界。
        inst:ListenForEvent("hutao_com_cosmic_hat_sys_for_player.save_hat",function(inst,player_despawn)
            if self.hat and self.hat:IsValid() then
                self:Set(self:GetHatRecordIndex().."_equipping",self.hat.components.equippable:IsEquipped())
                local hat_record = self.hat:GetSaveRecord()
                if player_despawn then
                    self.hat:Remove()
                end
                self:Set(self:GetHatRecordIndex(),hat_record)
                print("玩家离开当前世界。储存帽子。")
            end
        end)
        inst:ListenForEvent("player_despawn",function(inst)
            inst:PushEvent("hutao_com_cosmic_hat_sys_for_player.save_hat",true)
        end)
        self:AddOnSaveFn(function()
            inst:PushEvent("hutao_com_cosmic_hat_sys_for_player.save_hat")
        end)
    ------------------------------------------------------------
    ---- 玩家进入当前世界。
        inst:DoTaskInTime(0.2,function()
            ---- 储存了帽子，则恢复帽子
            local hat_record = self:Get(self:GetHatRecordIndex())
            if hat_record then
                local hat = SpawnSaveRecord(hat_record)
                self:Set(self:GetHatRecordIndex(),nil)  --- 清除记录
                self:SetHat(hat)
                if hat.components.hutao_com_cosmic_hat_sys_for_item then
                    hat.components.hutao_com_cosmic_hat_sys_for_item:LinkPlayer(self.inst)
                end
                if self:Get(self:GetHatRecordIndex().."_equipping",self.hat.components.equippable:IsEquipped())
                    and inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD) == nil then
                        inst.components.inventory:Equip(hat)
                else
                    inst.components.inventory:GiveItem(hat)
                    self:Set(self:GetHatRecordIndex().."_equipping",nil)
                end
                return
            end
            ---- 没有帽子，则寻找晾衣架
            self:find_linked_drying_rack()
            if self.linked_drying_rack == nil then
                self.inst:PushEvent("hutao_com_cosmic_hat_sys_for_player.hat_not_found")
            end
        end)
    ------------------------------------------------------------
end,
nil,
{
    hat = set_hat,
    drying_rack = set_drying_rack,
})
------------------------------------------------------------------------------------------------------------------------------
-- 储存字段。
    function hutao_com_cosmic_hat_sys_for_player:GetHatRecordIndex()
        -- if TheWorld:HasTag("cave") then
        --     return "hat_record.cave"
        -- else
        --     return "hat_record.overworld"
        -- end
        return "hat_record"
    end
------------------------------------------------------------------------------------------------------------------------------
-- 帽子相关
    function hutao_com_cosmic_hat_sys_for_player:SetHat(hat)
        self.hat = hat
        if hat and hat.components.hutao_com_cosmic_hat_sys_for_item then
            hat.components.hutao_com_cosmic_hat_sys_for_item:LinkPlayer(self.inst)
        end
    end
    function hutao_com_cosmic_hat_sys_for_player:HasHatInThisWorld()
        if self.hat and self.hat:IsValid() then
            return true
        end
        if self.linked_drying_rack and self.linked_drying_rack:IsValid() then
            return true
        end
        return false
    end
    --- 呼叫帽子
    function hutao_com_cosmic_hat_sys_for_player:CallHat()
        if self.hat and self.hat:IsValid() then
            local hat_record = self.hat:GetSaveRecord()
            self.hat:Remove()
            local hat = SpawnSaveRecord(hat_record)
            self:SetHat(hat)
            if hat.components.hutao_com_cosmic_hat_sys_for_item then
                hat.components.hutao_com_cosmic_hat_sys_for_item:LinkPlayer(self.inst)
            end
            self.inst.components.inventory:GiveItem(hat)
        elseif not self:HasHatInThisWorld() then
            self.inst:PushEvent("hutao_com_cosmic_hat_sys_for_player.call_hat_failed")
        elseif self.linked_drying_rack and self.linked_drying_rack:IsValid() then            
            print("帽子挂在晾衣架上。")
            self.inst:PushEvent("hutao_com_cosmic_hat_sys_for_player.call_hat_failed_because_drying_rack")
        end
    end
------------------------------------------------------------------------------------------------------------------------------
-- 获取链接的晾衣架
    function hutao_com_cosmic_hat_sys_for_player:find_linked_drying_rack()
        self.linked_drying_rack = nil
        local drying_rack_com = require("components/hutao_com_drying_module")
        if drying_rack_com then
            local linked_buildings = drying_rack_com:GetLinkingBuildings(self.inst) or {}
            if #linked_buildings > 0 then
                self.linked_drying_rack = linked_buildings[1]
            end
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function hutao_com_cosmic_hat_sys_for_player:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function hutao_com_cosmic_hat_sys_for_player:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function hutao_com_cosmic_hat_sys_for_player:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function hutao_com_cosmic_hat_sys_for_player:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存

    function hutao_com_cosmic_hat_sys_for_player:Get(index,default)
        if index then
            return self.DataTable[index] or default
        end
        return nil or default
    end
    function hutao_com_cosmic_hat_sys_for_player:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function hutao_com_cosmic_hat_sys_for_player:Add(index,num,min,max)
        if index then
            if min and max then
                local ret = (self.DataTable[index] or 0) + ( num or 0 )
                ret = math.clamp(ret,min,max)
                self.DataTable[index] = ret
                return ret
            else
                self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
                return self.DataTable[index]
            end
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
    function hutao_com_cosmic_hat_sys_for_player:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function hutao_com_cosmic_hat_sys_for_player:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_cosmic_hat_sys_for_player







